const commen = {}
import store from '@/store'
import timLogin from '@/utils/timLogin.js'

commen.emojiList = [
  [{
    'url': 'https://files.yzsheng.com/client/emoji/100.gif',
    alt: '[微笑]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/101.gif',
    alt: '[伤心]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/102.gif',
    alt: '[美女]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/103.gif',
    alt: '[发呆]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/104.gif',
    alt: '[墨镜]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/105.gif',
    alt: '[哭]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/106.gif',
    alt: '[羞]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/107.gif',
    alt: '[哑]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/108.gif',
    alt: '[睡]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/109.gif',
    alt: '[哭]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/110.gif',
    alt: '[囧]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/111.gif',
    alt: '[怒]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/112.gif',
    alt: '[调皮]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/113.gif',
    alt: '[笑]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/114.gif',
    alt: '[惊讶]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/115.gif',
    alt: '[难过]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/116.gif',
    alt: '[酷]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/117.gif',
    alt: '[汗]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/118.gif',
    alt: '[抓狂]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/119.gif',
    alt: '[吐]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/120.gif',
    alt: '[笑]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/121.gif',
    alt: '[快乐]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/122.gif',
    alt: '[奇]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/123.gif',
    alt: '[傲]'
  }],
  [{
    'url': 'https://files.yzsheng.com/client/emoji/124.gif',
    alt: '[饿]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/125.gif',
    alt: '[累]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/126.gif',
    alt: '[吓]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/127.gif',
    alt: '[汗]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/128.gif',
    alt: '[高兴]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/129.gif',
    alt: '[闲]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/130.gif',
    alt: '[努力]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/131.gif',
    alt: '[骂]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/132.gif',
    alt: '[疑问]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/133.gif',
    alt: '[秘密]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/134.gif',
    alt: '[乱]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/135.gif',
    alt: '[疯]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/136.gif',
    alt: '[哀]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/137.gif',
    alt: '[鬼]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/138.gif',
    alt: '[打击]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/139.gif',
    alt: '[bye]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/140.gif',
    alt: '[汗]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/141.gif',
    alt: '[抠]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/142.gif',
    alt: '[鼓掌]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/143.gif',
    alt: '[糟糕]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/144.gif',
    alt: '[恶搞]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/145.gif',
    alt: '[什么]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/146.gif',
    alt: '[什么]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/147.gif',
    alt: '[累]'
  }],
  [{
    'url': 'https://files.yzsheng.com/client/emoji/148.gif',
    alt: '[看]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/149.gif',
    alt: '[难过]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/150.gif',
    alt: '[难过]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/151.gif',
    alt: '[坏]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/152.gif',
    alt: '[亲]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/153.gif',
    alt: '[吓]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/154.gif',
    alt: '[可怜]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/155.gif',
    alt: '[刀]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/156.gif',
    alt: '[水果]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/157.gif',
    alt: '[酒]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/158.gif',
    alt: '[篮球]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/159.gif',
    alt: '[乒乓]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/160.gif',
    alt: '[咖啡]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/161.gif',
    alt: '[美食]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/162.gif',
    alt: '[动物]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/163.gif',
    alt: '[鲜花]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/164.gif',
    alt: '[枯]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/165.gif',
    alt: '[唇]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/166.gif',
    alt: '[爱]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/167.gif',
    alt: '[分手]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/168.gif',
    alt: '[生日]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/169.gif',
    alt: '[电]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/170.gif',
    alt: '[炸弹]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/171.gif',
    alt: '[刀子]'
  }],
  [{
    'url': 'https://files.yzsheng.com/client/emoji/172.gif',
    alt: '[足球]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/173.gif',
    alt: '[瓢虫]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/174.gif',
    alt: '[翔]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/175.gif',
    alt: '[月亮]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/176.gif',
    alt: '[太阳]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/177.gif',
    alt: '[礼物]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/178.gif',
    alt: '[抱抱]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/179.gif',
    alt: '[拇指]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/180.gif',
    alt: '[贬低]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/181.gif',
    alt: '[握手]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/182.gif',
    alt: '[剪刀手]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/183.gif',
    alt: '[抱拳]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/184.gif',
    alt: '[勾引]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/185.gif',
    alt: '[拳头]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/186.gif',
    alt: '[小拇指]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/187.gif',
    alt: '[拇指八]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/188.gif',
    alt: '[食指]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/189.gif',
    alt: '[ok]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/190.gif',
    alt: '[情侣]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/191.gif',
    alt: '[爱心]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/192.gif',
    alt: '[蹦哒]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/193.gif',
    alt: '[颤抖]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/194.gif',
    alt: '[怄气]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/195.gif',
    alt: '[跳舞]'
  }],
  [{
    'url': 'https://files.yzsheng.com/client/emoji/196.gif',
    alt: '[发呆]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/197.gif',
    alt: '[背着]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/198.gif',
    alt: '[伸手]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/199.gif',
    alt: '[耍帅]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/200.png',
    alt: '[微笑]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/201.png',
    alt: '[生病]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/202.png',
    alt: '[哭泣]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/203.png',
    alt: '[吐舌]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/204.png',
    alt: '[迷糊]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/205.png',
    alt: '[瞪眼]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/206.png',
    alt: '[恐怖]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/207.png',
    alt: '[忧愁]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/208.png',
    alt: '[眨眉]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/209.png',
    alt: '[闭眼]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/210.png',
    alt: '[鄙视]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/211.png',
    alt: '[阴暗]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/212.png',
    alt: '[小鬼]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/213.png',
    alt: '[礼物]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/214.png',
    alt: '[拜佛]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/215.png',
    alt: '[力量]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/216.png',
    alt: '[金钱]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/217.png',
    alt: '[蛋糕]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/218.png',
    alt: '[彩带]'
  }, {
    'url': 'https://files.yzsheng.com/client/emoji/219.png',
    alt: '[礼物]'
  }, ]
]

/**@dateTimeFliter 转换格林日期时间格式为常用日期格式
 * @time[必填] 						Date  		格林日期格式
 * @part[可选,默认:0]				Number      选择返回日期时间部分  列:0:返回所有 1:只返回日期  2:只返回时间
 * @dateComplete[可选,默认:true] 	Boolean 	日期位数不足是否添0补齐:true:补齐,false:不补齐
 * @timeComplete[可选,默认:true] 	Boolean 	时间位数不足是否添0补齐:true:补齐,false:不补齐
 * @dateConnector[可选,默认:-] 		String 		年月日连接符  例: - : /
 * @timeConnector[可选,默认::] 		String 		时间连接符   例: - : /
 * @hour12[可选,默认:false]          Boolean     是否返回12小时制时间   例: true:返回12小时制时间   false:返回24小时制时间
 * @return   '2019-11-25 15:05:54'  String    返回示例
 * **/
commen.dateTimeFliter = function(time, part = 0, dateComplete = true, timeComplete = true, dateConnector = '-',
  timeConnector = ':', hour12 = false) {
  let year = time.getFullYear()
  let month = time.getMonth() + 1
  let day = time.getDate()
  let hour = time.getHours()
  let minute = time.getMinutes()
  let second = time.getSeconds()
  let dateStr = ''
  let timeStr = ''
  //转换日期
  if (dateComplete) { //添0补齐
    if (month < 10) {
      month = '0' + month
    }
    if (day < 10) {
      day = '0' + day
    }
  }
  dateStr = year + dateConnector + month + dateConnector + day
  //转换时间
  //修改小时制
  if (hour12) {
    if (hour > 12) {
      hour = hour - 12
      if (timeComplete) {
        if (hour < 10) {
          hour = '下午 ' + '0' + hour
        } else {
          hour = '下午 ' + hour
        }
      }
    } else {
      if (timeComplete) {
        if (hour < 10) {
          hour = '上午 ' + '0' + hour
        } else {
          hour = '上午 ' + hour
        }
      }
    }
  }
  //判断分钟与秒
  if (timeComplete) { //添0补齐
    if (minute < 10) {
      minute = '0' + minute
    }
    if (second < 10) {
      second = '0' + second
    }
  }
  timeStr = hour + timeConnector + minute + timeConnector + second
  //合成输出值
  if (part == 0) {
    return dateStr + ' ' + timeStr
  } else if (part == 1) {
    return dateStr
  } else if (part == 2) {
    return timeStr
  }
  return '传参有误'
}

/** 获取当前位置
 * @return  Object 当前位置信息
 * **/
commen.getLocation = function(callback, type) {
  if (store.state.lon && store.state.lat && !type) {
    let loc = {
      lat: store.state.lat,
      lon: store.state.lon
    }
    store.commit('currentLocation', loc)
    callback && callback(loc)
    return false
  }
  if(type) {
    store.commit('currentLocation', {})
  }
  if (JSON.stringify(store.state.currentLocation) !== '{}') {
    callback && callback(store.state.currentLocation)
  } else {
    // #ifdef H5
    store.commit('currentLocation', {
      lat: 30.573095,
      lon: 104.066143
    })
    callback && callback(store.state.currentLocation)
    // #endif
    // #ifndef H5
    uni.getLocation({
      type: 'gcj02',
      geocode: true,
      success: res => {
        store.commit('currentLocation', {
          lat: res.latitude,
          lon: res.longitude
        })
        callback && callback(store.state.currentLocation)
      },
      fail: () => {
        store.commit('currentLocation', {
          lat: 30.573095,
          lon: 104.066143
        })
        callback && callback(store.state.currentLocation)
      }
    })
    // #endif
  }

}

/** 返回页面
 * **/
commen.loginSuccess = function() {
  // if (!getApp().globalData.redirect || getApp().globalData.redirect === '') {
  let pages = getCurrentPages()
  let current = null
  let num = 1
  let status = false
  let loginPage = null
  timLogin(() => {

  })
  if (pages.length > 1 && pages[0].route !== 'pages_logins/login/login') {
    pages.map((item, index) => {
      if (item.route === 'pages_logins/login/login' && !status) {
        num = pages.length - index
        current = pages[pages.length - index]
        status = true
        loginPage = item
      }
    })
    if(loginPage.options.mode && loginPage.options.mode === 'selfwash') {
      uni.redirectTo({
        url: '/pages_common/myLoveCarList/myLoveCarList?mode=selfwash'
      })
    } else {
      uni.navigateBack({
        delta: num
      })
    }
  } else {
    uni.reLaunch({
      url: '/pages/index/index'
    })
  }
  // }
}

// 倒计时
function checkTime(i){
  if(i<10){
    i = '0' + i
  }
  return i
}
commen.countDown = function(endTime){
  let time = endTime
  if(time < 1 ) return ['00', '00', '00']
  let second = Math.floor(time % 60)
  let minite = Math.floor((time / 60) % 60)
  let hour = Math.floor((time / 3600) % 24)
  let day = Math.floor((time / 3600) / 24)
  hour = checkTime(hour)
  minite = checkTime(minite)
  second = checkTime(second)
  return [hour, minite, second]
}

export default commen
